<?php

require_once 'opendb.php';
require_once 'setting.php';

class settingManager
{
    public static function addSetting($setting)
    {
        //$setting = mysql_real_escape_string($setting);
        $userID = $setting->getUserID();
        $calendar = $setting->getCalendar();
        $grades = $setting->getGrades();
        $links = $setting->getLinks();
        $facebook = $setting->getFacebook();
        $twitter = $setting->getTwitter();
        $tumblr = $setting->getTumblr();
        $linkedIn = $setting->getLinkedIn();
        $emailBool = $setting->getEmailBool();
        $textBool = $setting->getTextBool();
        
        $insert = mysql_query("INSERT INTO settings VALUES('$userID', '$calendar', '$grades', '$links', '$facebook', '$twitter', '$tumblr', '$linkedIn', '$emailBool', '$textBool')");
        $num = mysql_affected_rows();
        if ($num == 1)
            echo "The user's settings have been successfully added";
        else
            echo "The user's settings have not been successfully added.";
        echo "<br>";
    }

    public static function editSetting($setting)
    {
        //$setting = mysql_real_escape_string($setting);
        $userID = $setting->getUserID();
        $calendar = $setting->getCalendar();
        $grades = $setting->getGrades();
        $links = $setting->getLinks();
        $facebook = $setting->getFacebook();
        $twitter = $setting->getTwitter();
        $tumblr = $setting->getTumblr();
        $linkedIn = $setting->getLinkedIn();
        $emailBool = $setting->getEmailBool();
        $textBool = $setting->getTextBool();

        $update = mysql_query("UPDATE settings SET Calendar = '$calendar', Grades = '$grades', Links = '$links', Facebook = '$facebook', Twitter = '$twitter', Tumblr = '$tumblr', LinkedIn = '$linkedIn', EmailBool = '$emailBool', TextBool = '$textBool' WHERE UserID = '$userID' LIMIT 1") or die(mysql_error());
        $num = mysql_affected_rows();
        if ($num == 1)
            echo "Your settings have been successfully updated.";
        else
            echo "Your settings have not been successfully updated.";
        echo "<br>";
    }

    public static function getSettings($userID)
    {
        $userID = mysql_real_escape_string($userID);
        $select = mysql_query("SELECT * FROM settings WHERE UserID = '$userID'");
        $result = mysql_num_rows($select);
        if ($result > 0)
        {
            $settings = mysql_fetch_array($select);
            $setting = new Setting();
            $setting->setUserID($settings['UserID']);
            $setting->setCalendar($settings['Calendar']);
            $setting->setGrades($settings['Grades']);
            $setting->setLinks($settings['Links']);
            $setting->setFacebook($settings['Facebook']);
            $setting->setTwitter($settings['Twitter']);
            $setting->setTumblr($settings['Tumblr']);
            $setting->setLinkedIn($settings['LinkedIn']);
            $setting->setEmailBool($settings['EmailBool']);
            $setting->setTextBool($settings['TextBool']);

            return $setting;
        } else
            echo "This user's settings have not been set.<br>";
    }
}

?>